# encoding: utf-8


"""

@author: tongzhenguo

@time: 2020/9/14 上午10:46

@desc:

常见时间函数封装
"""
import arrow

now = arrow.now('Asia/Shanghai')


def now_date():
    return now


def now_date_ts():
    return now.timestamp


def now_date_fmt(fmt="YYYY-MM-DD HH:mm:ss"):
    return now.format(fmt=fmt)


def now_date_fmt_10():
    return now_date_fmt("YYYY-MM-DD")


def now_date_fmt_8():
    return now_date_fmt("YYYYMMDD")


def now_year():
    return now.format(fmt="YYYY")


def last_year():
    return now.replace(years=-1).format(fmt="YYYY")


def now_quarter():
    mm = now.format(fmt="MM")
    return 1 + (int(mm) - 1) // 3


def quarterly_report_date():
    """季度报最晚日期"""
    mm = int(now.format(fmt="MM"))
    if mm in (4, 5, 6):
        return now_year() + "0331"
    elif mm in (7, 8, 9):
        return now_year() + "0630"
    elif mm in (10, 11, 12):
        return now_year() + "0930"
    else:
        return now_year() + "1231"


def month_to_quarterly(mm):
    """月份对应季度"""
    if mm in (4, 5, 6):
        return 2
    elif mm in (7, 8, 9):
        return 3
    elif mm in (10, 11, 12):
        return 4
    else:
        return 1


def yearly_report_date():
    """年报最晚日期"""
    return last_year() + "1231"


def last_n_day(n, mode, **kwargs):
    if mode == "arrow":
        return now.shift(days=-n)
    elif mode == "timestamp":
        return now.shift(days=-n).timestamp
    elif mode == "str":
        if "format" in kwargs:
            return now.shift(days=-n).format(kwargs["format"])
        return now.shift(days=-n).format("YYYY-MM-DD HH:mm:ss")


def get_n_day(date8, n):
    date_arrow = arrow.get(date8, "YYYYMMDD").replace(tzinfo='local')
    target_date = date_arrow.shift(days=n).format("YYYYMMDD")
    return target_date


def last_n_year(n, mode, **kwargs):
    if mode == "arrow":
        return now.shift(years=-n)
    elif mode == "timestamp":
        return now.shift(years=-n).timestamp
    elif mode == "str":
        if "format" in kwargs:
            return now.shift(years=-n).format(kwargs["format"])
        return now.shift(years=-n).format("YYYY-MM-DD HH:mm:ss")


def last_n_hour(n, mode, **kwargs):
    if mode == "arrow":
        return now.shift(hours=-n)
    elif mode == "timestamp":
        return now.shift(hours=-n).timestamp
    elif mode == "str":
        if "format" in kwargs:
            return now.shift(hours=-n).format(kwargs["format"])
        return now.shift(hours=-n).format("YYYY-MM-DD HH:mm:ss")


def transform(date, src_mode, tar_mode, **kwargs):
    """
    各种时间类型转换，比如ts->时间字符串，时间字符串到时间
    :param date: arrow,时间戳(暂未实现),字符串
    :param src_mode:arrow，timestamp， str
    :param tar_mode:
    :param kwargs:
    :return: 如果类型不支持返回None,否则返回tar_mode对应的类型
    """
    date_arrow = None
    if src_mode == "arrow":
        date_arrow = date
    elif src_mode == "timestamp":
        pass
    elif src_mode == "str":
        if "src_format" in kwargs:
            # 默认utc时间，需要减8个小时；或者指定使用本地时区
            date_arrow = arrow.get(date, kwargs['src_format']).replace(tzinfo='local')
        else:
            pass
    if tar_mode == "arrow":
        return date_arrow
    elif tar_mode == "timestamp":
        return date_arrow.timestamp
    elif tar_mode == "str":
        if "tar_format" in kwargs:
            return date_arrow.format(kwargs["tar_format"])
        else:
            return date_arrow
